(function(){
	var pathFinder, root, btp;
	pathFinder = new PathFinder();
	root = pathFinder.getRootPath('script', 'src', 'extensions/Dplayer/player.js') ||
		pathFinder.getRootPath('script', 'src', /extensions\/LowCalVanilla\/packer\.php.*$/) ||
		'';
	
	// For compatibility, in case on extension were using it
	if(typeof(window.btp) === 'undefined') {
		window.btp = {}
	}
	btp = window.btp;
	
	btp.Mp3 = {
		playimg: null,
		player: null,
		go: function() {
			var img,
				all = document.getElementsByTagName('a');
			for (var i = 0, o; o = all[i]; i=i+1) 
			{
				if(o.href.match(/\.mp3$/i)) 
				{
					img = document.createElement('img');
					img.src = root+'extensions/Dplayer/off.gif';
					img.title = '';
					img.height = 27;
					img.width = 74;
					img.style.marginRight = '0.2em';
					img.style.cursor = 'pointer';
					img.style.verticalAlign = 'middle';
					img.style.border = '0';
					img.onclick = btp.Mp3.makeToggle(img, o.href);
					o.parentNode.insertBefore(img, o);
				}
			}
		},
		toggle: function(img, url) 
		{
			if (btp.Mp3.playimg === img) {
				btp.Mp3.destroy();
			} else {
				if (btp.Mp3.playimg) {
					btp.Mp3.destroy();
				}
				var a, c, c16, j, hex, hexSm, rgb;
				a = img.nextSibling;
				if (document.defaultView &&
					document.defaultView.getComputedStyle)
				{
					c = document.defaultView.getComputedStyle(a,null).getPropertyValue('color');
				} else if (a.currentStyle) {
					c = a.currentStyle.color;
				}
				c = c.replace(/^rgb|[ ()]/g, '');
				hex = c.match(/^#?([a-f0-9]{6})$/);
				hexSm = c.match(/^#?([a-f0-9]{3})$/);
				rgb = c.match(/^([0-9]+),([0-9]+),([0-9]+)$/);
				if (rgb) {
					c16 = "0123456789abcdef";
					c='';
					for(var i=1,d;d=rgb[i];i=i+1) {
						j=d%16;
						c = c +
							c16.charAt((d-j)/16) +
							c16.charAt(j);
					}
				} else if (hex) {
					c = hex[1];
				} else if (hexSm) {
					c = hexSm[1].charAt(0) +
						hexSm[1].charAt(0) +
						hexSm[1].charAt(1) + 
						hexSm[1].charAt(1) + 
						hexSm[1].charAt(2) + 
						exSm[1].charAt(2);
				} else {
					c = "000000";
				}
				img.src = root+'extensions/Dplayer/on.gif'; 
				img.title = ''; 
				btp.Mp3.playimg = img;
				btp.Mp3.player = document.createElement('span');
				btp.Mp3.player.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
					'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' +
					'width="1" height="1" id="player" align="bottom">' +
					'<param name="wmode" value="transparent" />' +
					'<param name="allowScriptAccess" value="sameDomain" />' +
					'<param name="flashVars" value="theLink='+url+'&amp;fontColor='+c+'" />' +
					'<param name="movie" value="'+root+'extensions/Dplayer/player.swf" />' +
					'<embed src="'+root+'extensions/Dplayer/player.swf" flashVars="theLink='+url+'&amp;fontColor='+c+'"'+
					'wmode="transparent" width="1" height="1" name="player"' +
					'align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"' +
					' pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
				img.parentNode.appendChild(btp.Mp3.player, img.nextSibling);
		}},
		destroy: function() {
			btp.Mp3.playimg.src = root+'extensions/Dplayer/off.gif';
			btp.Mp3.playimg = null;
			btp.Mp3.player.removeChild(btp.Mp3.player.firstChild);
			btp.Mp3.player.parentNode.removeChild(btp.Mp3.player);
			btp.Mp3.player = null;
		},
		makeToggle: function(img, url) {
			return function(){ 
				btp.Mp3.toggle(img, url);
			};
		}
	};
	
	btp.addLoadEvent = function(f) {
		var old = window.onload;
		if (typeof old !== 'function') {
			window.onload = f;
		} else {
			window.onload = function() {
				old();
				f();
			};
		}
	};
	
	btp.addLoadEvent(btp.Mp3.go);
})();
